<?php

namespace App\Services\Admin;

use App\Models\Teacher as TeacherModel;
use App\Models\ClassBase as ClassBaseModel;
use App\Models\Classroom as ClassroomModel;
use App\Models\ClassBaseTime as ClassBaseTimeModel;


class Common extends Base
{

    public static function class_base_time()
    {
        $week = request()->week ?? 0;

        $result = [
            'base_time'=>self::base_time($week)
        ];

        return $result;
    }

    public static function base_teacher_classroom()
    {
        $store_id = request()->store_id ?? 0;

        $result = [
            'base' => self::base($store_id),
            'teacher' => self::teacher($store_id),
            'classroom' => self::classroom($store_id),
        ];

        return $result;
    }

    public static function teacher($store_id = 0)
    {
        $teacher = TeacherModel::display();
        if (!empty($store_id)) $teacher = $teacher->where('store_id', $store_id);

        //查询已预约过的老师
        $class_teacher_id = Reservation::class_teacher_id();
        if(!empty($class_teacher_id))$teacher = $teacher->whereNotIn('id', $class_teacher_id);

        $teacher = $teacher->select('teacher_name', 'id')->get()->toArray();

        return $teacher;
    }

    public static function base($store_id = 0)
    {
        $class_base_numbering_id = request()->class_base_numbering_id ?? 0;

        $class_base = (new ClassBaseModel());
        if (!empty($store_id)) $class_base = $class_base->where('store_id', $store_id);
        if (!empty($class_base_numbering_id)) $class_base = $class_base->where('class_base_numbering_id', $class_base_numbering_id);
        $class_base = $class_base->select('class_name', 'id')->get()->toArray();

        return $class_base;
    }

    public static function classroom($store_id = 0)
    {
        $classroom = (new ClassroomModel());
        if (!empty($store_id)) $classroom = $classroom->where('store_id', $store_id);
        $classroom = $classroom->select('numbering', 'id')->get()->toArray();

        return $classroom;
    }

    public static function base_time($week = 0)
    {
        $class_base_time = (new ClassBaseTimeModel());
        if (!empty($week)) $class_base_time = $class_base_time->where('week', $week);
        $class_base_time = $class_base_time->select('time_start', 'time_end', 'id', 'duration')->orderBy('time_start','asc')->get()->toArray();

        foreach ($class_base_time as &$v){
            $v['class_time'] = "{$v['time_start']}~{$v['time_end']}";
        }

        return $class_base_time;
    }
}
